#define _CRT_SECURE_NO_WARNINGS 1
#include"Sort.h"
void Swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}
void Print(int* a,int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}
void InsertSort(int* a, int n)
{
	for (int i = 0; i < n-1; i++)
	{
		int end=i;
		int temp = a[end + 1];
		while (end >= 0)
		{
			if (temp < a[end])
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1]=temp;
	}
}
void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int temp = a[end + gap];
			while (end >= 0)
			{
				if (temp < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = temp;
		}
	}
}
void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int max = begin, min = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			if (a[i] > a[max])
			{
				max = i;
			}
			if (a[i] < a[min])
			{
				min = i;
			}
		}
		if (begin == max)
		{
			max = min;
		}
		Swap(&a[begin], &a[min]);
		Swap(&a[end], &a[max]);
		begin++;
		max--;
	}
}
int _QuickSort(int* a, int left, int right)
{
	int key = left;
	left++;
	while (left <= right)
	{
		while (left <= right && a[right] > a[key])
		{
			right--;
		}
		while (left <= right && a[left] < a[key])
		{
			left++;
		}
		if (left <= right)
		{
			Swap(&a[left++], &a[right--]);
		}
	}
	Swap(&a[key], &a[right]);

	return right;
}
void QuickSort(int* a, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int key = _QuickSort(a, left, right);
	QuickSort(a, left, key - 1);
	QuickSort(a, key + 1, right);
}